package www.shuang.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import www.shuang.common.controller.BaseController;
import www.shuang.common.util.ConstatFinalUtil;
import www.shuang.common.util.PageInfoUtil;
import www.shuang.users.pojo.AAdmins;
import www.shuang.users.service.IAAdminsService;

@Controller
@RequestMapping("/back/admins/")
public class AdminsBackController extends BaseController
{			@Resource
			private IAAdminsService adminsService;
			@RequestMapping("/index.htm")
			public String index(){
				
				return "/back/index";
			}
			
			//左侧菜单
			@RequestMapping("/leftMenu.htm")
			public String leftMenu(){
				
				return "/back/leftMenu";
			}
			//退出登陆系统
			@RequestMapping("/logOut.htm")
			public String logOut(HttpServletRequest request){
				HttpSession session=request.getSession();
				session.removeAttribute("admins");
				session.removeAttribute("lastLoginTime");
				
				return "redirect:/adminsLogin.htm";
			}
			//管理员列表
			@RequestMapping("/adminsList.htm")
			public String adminsList(HttpServletRequest request){
				ConstatFinalUtil.LOGGER_SYSTEM.info("----------");
				//接收参数
				String keyword=request.getParameter("keyword");
				String status=request.getParameter("status");
				String st=request.getParameter("st");
				String ed=request.getParameter("ed");
				/*if(status == null){
					status="";
				}*/
				if(st == null){
					st="";
				}
				if(ed == null){
					ed="";
				}
				Date stDate =null;
				Date edDate=null;
				if(!"".equalsIgnoreCase(st)&&!"".equalsIgnoreCase(ed)){
					stDate=this.dateFormatUtil.parseDate(st);
					edDate=this.dateFormatUtil.parseDate(ed);
				}
				PageInfoUtil pageInfoUtil =this.proccedPage(request);
				Map<String,Object> condMap=new HashMap<String,Object>();
				condMap.put("keyword", keyword);
				condMap.put("status", status);
				condMap.put("stDate", stDate);
				condMap.put("edDate", edDate);
				//
				List<AAdmins> adminsList=this.adminsService.findCondListAAdminsService(pageInfoUtil, condMap);
				request.setAttribute("adminsList", adminsList);
				request.setAttribute("pageInfoUtil", pageInfoUtil);
				//存储条件
				request.setAttribute("keyword", keyword);
				request.setAttribute("status", status);
				request.setAttribute("st", st);
				request.setAttribute("ed", ed);
				return "/back/adminsList";
			}
			//打开管理员添加页面
			@RequestMapping("/adminsInsert.htm")
			public String adminsInsert(){
				
				return "/back/adminsInsert";
			}
			//管理员添加提交页面
			@RequestMapping(value="/adminsInsertSubmit.htm",produces="text/html;charset=UTF-8")
			public @ResponseBody String adminsInsertSubmit(AAdmins admins,HttpServletRequest request){
				//设置密码
				admins.setPassword(this.encryptUtil.encryString(admins.getPassword()));
				admins.setCreateTime(new Date());
				admins.setUpdateTime(new Date());
				admins.setLastLoginTime(new Date());
				JSONObject resultJSON = this.adminsService.insertOneAAdminsService(admins);
				ConstatFinalUtil.LOGGER_SYSTEM.info("----"+resultJSON);
				resultJSON=this.returnJuiJSON(request, resultJSON.getString("info"));
				return resultJSON.toJSONString();
			}
}
